/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "filebackupmanagedlg.h"
#include "ui_filebackupmanagedlg.h"
#include "powermanager.h"
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <qdebug.h>

FileBackupManageDlg::FileBackupManageDlg(PowerManager *p_powerMangerIn,UserInfo userInfoIn,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FileBackupManageDlg)
{
    ui->setupUi(this);
    p_powerManger=p_powerMangerIn;
    userInfo=userInfoIn;
}

FileBackupManageDlg::~FileBackupManageDlg()
{
    delete ui;
}

void FileBackupManageDlg::on_pushButton_clicked()
{
    //p_powerManger->backupControllerProgram()
    int backupType=ui->comboBox_2->currentIndex();
    int diskType=ui->comboBox->currentIndex();
    int robotId=ui->comboBox_3->currentIndex()+1;

    string fileName=ui->lineEdit->text().toStdString();
    int returnFlag;


    p_powerManger->backupFile(userInfo,backupType,diskType,fileName,robotId,returnFlag);
    if(0==returnFlag)
    {
        QMessageBox::information(NULL, "information", "backup成功！", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "information", "backup失败！", QMessageBox::Yes );
    }
}

void FileBackupManageDlg::on_pushButton_2_clicked()
{
    int backupType=ui->comboBox_2->currentIndex();
    int diskType=ui->comboBox->currentIndex();
    int robotId=ui->comboBox_3->currentIndex();

    string fileName=ui->lineEdit->text().toStdString();
    int returnFlag;


    p_powerManger->restoreFile(userInfo,backupType,diskType,fileName,robotId,returnFlag);
    if(0==returnFlag)
    {
        QMessageBox::information(NULL, "information", "restore成功！", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "information", "restore失败！", QMessageBox::Yes );
    }
}

void FileBackupManageDlg::on_pushButton_3_clicked()
{
    //定义文件对话框类
       QFileDialog *fileDialog = new QFileDialog(this);
       //定义文件对话框标题
       fileDialog->setWindowTitle(tr("选择文件"));
       //设置默认文件路径
       fileDialog->setDirectory(PC_HARD_DISK_BACKUP_PATH);
       //设置文件过滤器
       //fileDialog->setNameFilter(tr("Images(*.png *.jpg *.jpeg *.bmp)"));
       //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
       fileDialog->setFileMode(QFileDialog::ExistingFiles);
       //设置视图模式
       fileDialog->setViewMode(QFileDialog::Detail);
       //打印所有选择的文件的路径
       QStringList fileNames;
       if(fileDialog->exec())
       {
           fileNames = fileDialog->selectedFiles();
       }
        qDebug()<<fileNames;
        if(fileNames.size()>0)
        {
            ui->lineEdit->setText(fileNames[0].section("/",3,3));
        }
}

void FileBackupManageDlg::on_pushButton_4_clicked()
{
    ui->listWidget->clear();
    int backupType=ui->comboBox->currentIndex();
    vector<string> fileList;
    p_powerManger->getBackupFileList(userInfo,backupType,fileList);
    for(int i=0;i<fileList.size();i++)
    {
        ui->listWidget->addItem(QString::fromStdString(fileList[i]));
    }

}

void FileBackupManageDlg::on_listWidget_clicked(const QModelIndex &index)
{
    ui->lineEdit->setText(ui->listWidget->currentItem()->text());
}

void FileBackupManageDlg::on_pushButton_restore_original_clicked()
{
    QMessageBox message(QMessageBox::NoIcon, "Show Qt", "确定要恢复出厂状态吗，现有信息将会丢失?"
                        , QMessageBox::Yes | QMessageBox::No, NULL);
    if(message.exec() == QMessageBox::Yes)
    {
        int backupType=6;
        int diskType=0;
        int robotId=ui->comboBox_3->currentIndex();

        string fileName="controller_original.zip";
        int returnFlag;

        p_powerManger->restoreFile(userInfo,backupType,diskType,fileName,robotId,returnFlag);
        if(0==returnFlag)
        {
            QMessageBox::information(NULL, "information", "restore成功！", QMessageBox::Yes );
        }
        else
        {
            QMessageBox::information(NULL, "information", "restore失败！", QMessageBox::Yes );
        }
    }



}
